#include "FileTest.h"
#include <stdio.h>
#include <string.h>

/*
 * Created: 2/24/12; CGP
 
 a) Create a new file (opened for writing)
 b) Write enough data into the file so that the data spans at least 2 Disk Blocks. 
    Write a known pattern of bytes with at least some different bytes 
    (e.g., don't write all 0 values to the file). Use multiple write calls 
    (e.g., write 100 bytes each time).
 c) Close the file
 d) Open the same file for reading. 
 e) Read all the data & verify that the data was the same as expected. 
    Use multiple read calls (e.g., read 100 bytes each time).
 f) Make sure that when you read past the end of the file, you can't read more data. 
    (i.e., End of File is detected correctly).
 */

#define NUMBER_WRITES 38

int main() {
    int length;
    printf("Starting test...\n"); fflush(stdout);
    FileTest ft;
    const char *fileName = "test1";

    File *f1 = new File(fileName, "w");
    unsigned char data[] = "abcdefghijklmnopqrstuvwxyz";
    int dataLength = strlen((const char *) data)+1;
    for (int i=0; i < NUMBER_WRITES; i++) { // 38*27 bytes = 1026 bytes
        length = f1->Write(data, dataLength);
    }
    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;

    f1 = new File(fileName, "r");
    unsigned char buffer[100];
    for (int i=0; i < NUMBER_WRITES; i++) { // 38*27 bytes = 1026 bytes
        length = f1->Read(buffer, dataLength);
        
        if (length != dataLength) {
            printf("ERROR: Could not read back all %d bytes of file\n", dataLength);
        }
        
        if (strcmp((const char *) data, (const char *) buffer) != 0) {
            printf("ERROR: Buffer (= %s) is not equal to data written (= %s)\n", buffer, data);
        }
    }

    length = f1->Read(buffer, 1);
    if (length != 0) {
        printf("ERROR: End of file not correctly reached\n");
    }

    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;
}




